package com.flyox.game.militarychess.service.astar;

import java.util.HashMap;
import java.util.LinkedList;

import com.flyox.game.militarychess.bean.ChessMan;

/**
 * 工兵寻径算法
 * @author sunwei
 */
public class FindPath {

	private LinkedList<PosBean> curPath;// 当前路径
	private HashMap<String, PosBean> closedPos;// 以删除节点
	ChessMan[][] layout;
	
	public FindPath(ChessMan[][] layout){
		this.layout=layout;
	}
	
	private boolean isCurrPathContain(int x, int y) {
		for (PosBean pb : curPath) {
			if (pb.getRow() == x && pb.getCol() == y) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 如果返回值 curpath为empty 说明不能到达
	 * 工兵的寻径
	 * @param beginPos
	 * @param endPos
	 * @return
	 */
	public LinkedList<PosBean> searchPath(PosBean beginPos, PosBean endPos) {
		curPath = new LinkedList<PosBean>();
		closedPos = new HashMap<String, PosBean>();
		PosBean curPos;
		// check paramter
		if (beginPos == null || beginPos == null) {
			return curPath;
		}
		if (beginPos.equals(endPos)) {
			return curPath;
		}
		// 堆栈 先进后出
		curPath.addLast(beginPos);
		
		//是否在一步之内到达
		if(Math.abs(beginPos.getRow()-endPos.getRow())==1||Math.abs(beginPos.getCol()-endPos.getCol())==1){
			curPath.add(endPos);
			return curPath;
		}
		
		endPos.setLayout(layout);
		while (!curPath.isEmpty()) {
			curPos = curPath.getFirst();
			curPos.setLayout(layout);//must
			
			int preCol=curPos.getCol()-1;//move left
			int nextCol=curPos.getCol()+1;//move right
			if(curPos.row==6&&(preCol==1||preCol==3||preCol==6||preCol==8||preCol==11||preCol==13||preCol==16||preCol==18)){
				preCol--;
			}
			if(curPos.row==6&&(nextCol==1||nextCol==3||nextCol==6||nextCol==8||nextCol==11||nextCol==13||nextCol==16||nextCol==18)){
				nextCol++;
			}
			
			if (curPos.canMoveLeft(endPos) && !closedPos.containsKey("" + curPos.getRow() + "_" + preCol) && !isCurrPathContain(curPos.getRow(), preCol)) {
				PosBean nextPos = new PosBean(curPos.getRow(), preCol);
				curPath.addFirst(nextPos);
				if (nextPos.row==endPos.row&&nextPos.col==endPos.col) {
					return curPath;
				} else {
					continue;
				}
			} else if (curPos.canMoveRight(endPos) && !closedPos.containsKey("" + curPos.getRow() + "_" + nextCol) && !isCurrPathContain(curPos.getRow(), nextCol)) {
				PosBean nextPos = new PosBean(curPos.getRow(), nextCol);
				curPath.addFirst(nextPos);
				if (nextPos.row==endPos.row&&nextPos.col==endPos.col) {
					return curPath;
				} else {
					continue;
				}
			} else if (curPos.canMoveUp(endPos) && !closedPos.containsKey("" + (curPos.getRow() -1) + "_" + curPos.getCol()) && !isCurrPathContain(curPos.getRow()-1, curPos.getCol())) {
				PosBean nextPos = new PosBean(curPos.getRow()-1, curPos.getCol());
				curPath.addFirst(nextPos);
				if (nextPos.row==endPos.row&&nextPos.col==endPos.col) {
					return curPath;
				} else {
					continue;
				}
			} else if (curPos.canMoveDown(endPos) && !closedPos.containsKey("" + (curPos.getRow()+1) + "_" + curPos.getCol()) && !isCurrPathContain(curPos.getRow()+1, curPos.getCol())) {
				PosBean nextPos = new PosBean(curPos.getRow()+1, curPos.getCol());
				curPath.addFirst(nextPos);
				if (nextPos.row==endPos.row&&nextPos.col==endPos.col) {
					return curPath;
				} else {
					continue;
				}
			} else {// 上下左右都不可以走 删除当前位置并把当前位置放到删除表当中
				PosBean nextPos = curPath.removeFirst();
				closedPos.put(nextPos.toString(), nextPos);
			}
		}
		return curPath;
	}

}
